package com.example.administrator.yuejian35.Home.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.yuejian35.Home.HomeModel;
import com.example.administrator.yuejian35.Home.bean.Result;
import com.example.administrator.yuejian35.MainActivity;
import com.example.administrator.yuejian35.R;
import com.example.administrator.yuejian35.base.BaseActivity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ApplyActivity extends BaseActivity {
    private ImageButton add,down,sadd,sdown,back;
    private Button submit;
    private EditText adult,student;
    private TextView money,aname,amoney,smoney,apply_thing;
    private String aid,name,Uid,notes;
    private int am,sm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply);
        aname= (TextView) findViewById(R.id.apply_name);
        adult = (EditText) findViewById(R.id.edit_adult);
        student = (EditText) findViewById(R.id.edit_student);
        add = (ImageButton) findViewById(R.id.imgbtn_add);
        down = (ImageButton) findViewById(R.id.imgbtn_down);
        sadd = (ImageButton) findViewById(R.id.imgbtn_sadd);
        sdown = (ImageButton) findViewById(R.id.imgbtn_sdown);
        back = (ImageButton) findViewById(R.id.apply_btn_back);
        submit = (Button) findViewById(R.id.apply_btn_submit);
        money = (TextView) findViewById(R.id.apply_money);
        amoney = (TextView) findViewById(R.id.apply_amoney);
        smoney = (TextView) findViewById(R.id.apply_smoney);
        apply_thing = (TextView) findViewById(R.id.apply_thing);

        aid = getIntent().getStringExtra("aid");
        name = getIntent().getStringExtra("aname");
        am = getIntent().getIntExtra("am",0);
        sm = getIntent().getIntExtra("sm",0);
        notes = getIntent().getStringExtra("notes");

        amoney.setText(String.valueOf(am));
        smoney.setText(String.valueOf(sm));
        aname.setText(name);
        apply_thing.setText(notes);
        sum();

        MainActivity mainActivity = new MainActivity();
        Uid = mainActivity.getUserId();

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int number = Integer.parseInt(String.valueOf(adult.getText()));
                int num = number+1;
                adult.setText(String.valueOf(num));
                sum();
                //Toast.makeText(ApplyActivity.this,num,Toast.LENGTH_SHORT).show();
            }
        });

        down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int number = Integer.parseInt(String.valueOf(adult.getText()));
                int num = number-1;
                adult.setText(String.valueOf(num));
                sum();
                //Toast.makeText(ApplyActivity.this,num,Toast.LENGTH_SHORT).show();
            }
        });


        sadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int number = Integer.parseInt(String.valueOf(student.getText()));
                int num = number+1;
                student.setText(String.valueOf(num));
                sum();
                //Toast.makeText(ApplyActivity.this,num,Toast.LENGTH_SHORT).show();
            }
        });

        sdown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int number = Integer.parseInt(String.valueOf(student.getText()));
                int num = number-1;
                student.setText(String.valueOf(num));
                sum();
                //Toast.makeText(ApplyActivity.this,num,Toast.LENGTH_SHORT).show();
            }
        });

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String msum = String.valueOf(money.getText());
                int psum = Integer.parseInt(String.valueOf(adult.getText()))+Integer.parseInt(String.valueOf(student.getText()));

                HomeModel homeModel = new HomeModel();
                Call call = homeModel.apply_activity(aid,Uid,psum,msum);

                call.enqueue(new Callback<Result>() {

                    @Override
                    public void onResponse(Call<Result> call, Response<Result> response) {
                        if (response.body().getResult().equals("success")){
                            Toast.makeText(ApplyActivity.this,"报名成功！！！",Toast.LENGTH_SHORT).show();

                            finish();
                        }
                        else {
                            Toast.makeText(ApplyActivity.this,"报名失败！！！",Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onFailure(Call<Result> call, Throwable t) {
                        Toast.makeText(ApplyActivity.this,"联网失败！！！！",Toast.LENGTH_SHORT).show();
                    }
                });

            }
        });


    }

    public void sum(){
        int sum = Integer.parseInt(String.valueOf(student.getText()))*sm+Integer.parseInt(String.valueOf(adult.getText()))*am;
        money.setText(String.valueOf(sum));
    }

}
